package com.fhj.utils;

import android.app.ActivityManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.LruCache;
import android.widget.ImageView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.fhj.movie_fhj_01.R;

public class ImageDownload{
	
	  private static ImageDownload instance;
	  private ImageLoader.ImageCache mImageCache;
	  private ImageLoader mImageLoader;
	  private RequestQueue mQueue;
	  
	  private ImageDownload(Context paramContext)
	  {
		this.mImageCache = new BitmapCache();
	    this.mQueue = Volley.newRequestQueue(paramContext);
	    this.mImageLoader = new ImageLoader(this.mQueue, this.mImageCache);
	  }

	  public static ImageDownload getInstance(Context paramContext)
	  {
	    if (instance == null)
	      return new ImageDownload(paramContext);
	    return instance;
	  }

	  public void downLoadImage(ImageView paramImageView, String paramString)
	  {
	    ImageLoader.ImageListener localImageListener = ImageLoader.getImageListener(paramImageView, R.drawable.place_holder, R.drawable.place_holder);
	    this.mImageLoader.get(paramString, localImageListener);
	  }

	  public void downLoadImage(NetworkImageView paramNetworkImageView, String paramString)
	  {
	    paramNetworkImageView.setImageUrl(paramString, this.mImageLoader);
	  }
	  class BitmapCache implements ImageLoader.ImageCache{
		  private LruCache<String, Bitmap> mCache;    
          
	        public BitmapCache() {    
	            int maxSize = 1024 * 1024*10;    
	            mCache = new LruCache<String, Bitmap>(maxSize) {    
	                @Override    
	                protected int sizeOf(String key, Bitmap value) {    
	                    return value.getRowBytes() * value.getHeight();    
	                }    
	                    
	            };    
	        }    
		@Override
		public Bitmap getBitmap(String arg0) {
			// TODO Auto-generated method stub
			return mCache.get(arg0);
		}

		@Override
		public void putBitmap(String arg0, Bitmap arg1) {
			// TODO Auto-generated method stub
			mCache.put(arg0, arg1);
		}
		  
	  }
	}
